package com.aaa.multithreading.demo03;


/**
 * @author C-Q
 * @version 1.1.0
 * @Description: 使用多线程模拟车站中四个窗口同时出售100张车票
 * @date 2022/10/31
 */
public class SellTickets01 implements Runnable {
    //创建自动锁对象
    private static Object lock = new Object();
    /**
     * 模拟的100张票
     */
    private int ticket = 100;

    @Override
    public void run() {

        while (true) {
            synchronized (lock) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + "出售一张票" + --ticket);
                } else {
                    break;
                }
            }

        }

    }
}